package com.woniu.controller;

import com.woniu.dto.CartsDTO;
import com.woniu.dto.OrderDTO;
import com.woniu.dto.OrdersDTO;
import com.woniu.entity.*;
import com.woniu.service.AllService;
import com.woniu.util.ResponseUtil;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/api")
public class AllController {

    @Resource
    private AllService allService;

    /* 1. 登录 */
    @PostMapping("/login")
    public ResponseUtil login(@RequestBody Map<String,String> map){
        String username = map.get("username");
        String password = map.get("password");
        return allService.login(username, password);
    }

    /* 2. 商品列表 */
    @GetMapping("/products")
    public ResponseUtil products(@RequestParam(required = false) Integer categoryId,
                                 @RequestParam(defaultValue = "1") Integer page,
                                 @RequestParam(defaultValue = "10") Integer size) {
        return allService.listProducts(categoryId, page, size);
    }

    /* 3. 商品详情 */
    @GetMapping("/products/{productId}")
    public ResponseUtil product(@PathVariable Integer productId) {
        return allService.getProduct(productId);
    }

    /* 4. 购物车 */
    @PostMapping("/carts")
    public ResponseUtil addCart(@RequestBody Cart cart) {
        return allService.addCart(cart);
    }

    @GetMapping("/carts/{customerId}")
    public ResponseUtil listCart(@PathVariable Integer customerId) {
        return allService.listCart(customerId);
    }

    @DeleteMapping("/carts/{cartId}")
    public ResponseUtil deleteCart(@PathVariable Integer cartId) {
        return allService.deleteCart(cartId);
    }

    @PutMapping("/carts")
    public ResponseUtil updateCart(@RequestBody CartsDTO dto) {
        return allService.updateCart(dto.getCartId(), dto.getBuyNum());
    }

    /* 5. 下单 */
    @PostMapping("/orders")
    public ResponseUtil createOrder(@RequestBody OrdersDTO dto) {
        Order o = new Order();
        o.setCustomerId(dto.getCustomerId());
        o.setAddressId(dto.getAddressId());
        o.setTotalPrice(dto.getItems().stream()
                .map(i -> i.getPrice().multiply(BigDecimal.valueOf(i.getBuyNum())))
                .reduce(BigDecimal.ZERO, BigDecimal::add));

        // 将 DTO 转换为 OrderItem 实体列表
        List<OrderItem> items = dto.getItems().stream()
                .map(i -> {
                    OrderItem item = new OrderItem();
                    item.setProductId(i.getProductId());
                    item.setName(i.getName());
                    item.setBuyNum(i.getBuyNum());
                    item.setPrice(i.getPrice());
                    return item;
                })
                .collect(Collectors.toList());

        return allService.createOrder(o, items, dto.getCustomerId());
    }

    /*  订单详情 */
    @GetMapping("/orders/{orderId}")
    public ResponseUtil orderDetail(@PathVariable String orderId) {
        return allService.orderDetail(orderId);
    }
    /*  确认收货 */
    @PutMapping("/orders/receive")
    public ResponseUtil confirmReceive(@RequestBody Map<String,String> body) {
        String orderId = body.get("orderId");
        return allService.confirmReceive(orderId);
    }

    /*  退单审核 */
    @PutMapping("/orders/refund")
    public ResponseUtil refundAudit(@RequestBody Map<String,String> body) {
        String orderId = body.get("orderId");
        return allService.refundAudit(orderId);
    }




    /* 6. 个人中心 */
    @GetMapping("/customers/{customerId}")
    public ResponseUtil customer(@PathVariable Integer customerId) {
        return allService.getCustomer(customerId);
    }

    @GetMapping("/orders/customer/{customerId}")
    public ResponseUtil orders(@PathVariable Integer customerId) {
        return allService.listOrders(customerId);
    }


    @GetMapping("/addresses")
    public ResponseUtil listAddress(@RequestParam Integer customerId,
                                    @RequestParam(required = false) Integer defaultOnly) {
        return allService.listAddress(customerId, defaultOnly);
    }
}
